package cn.lili.controller.buyer.payment;

import cn.lili.common.enums.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.payment.kit.dto.PayParam;
import cn.lili.modules.payment.kit.dto.paypal.CheckoutDto;
import cn.lili.modules.payment.kit.paypal.CreateOrder;
import cn.lili.modules.payment.service.PayPalCheckoutService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@Api(description = "PayPalCheckout接口")
@RequestMapping("/buyer/paypal")
public class PayPalController {

    @Autowired
    private CreateOrder createOrder;
    @Autowired
    private PayPalCheckoutService payPalCheckoutService;

    @PostMapping(value = "/createOrder")
    public ResultMessage callback(PayParam payParam) throws Exception {
        String url = createOrder.createOrder(payParam);
        return ResultUtil.data(url);
    }

    @ApiOperation(value = "ipn异步回调")
    @PostMapping(value = "/ipn/back", produces = "application/json")
    public String callback(@RequestBody CheckoutDto checkoutDto) throws Exception {
        log.info("PayPalController.callback:{}", checkoutDto);
        return payPalCheckoutService.callback(checkoutDto);
    }
}
